package com.asiainfo.indb.control.db.pool;

import com.asiainfo.indb.control.db.DialectFactory;
import com.asiainfo.indb.control.db.IDialect;
import com.asiainfo.indb.model.InDbGather;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 简单的连接池jdbc直接获取
 *
 * @author zym
 * @date 2019-10-18 14:40
 */
public class JdbcPool implements IDbPool {

    private String user;
    private String password;
    private String url;

    private IDialect dialect;

    @Override
    public IDbPool init(InDbGather dbData) {
        url = dbData.get("DB_URL");
        user = dbData.get("DB_USER");
        password = dbData.get("DB_PASSWORD");

        dialect = DialectFactory.getDialect(dbData.get("DB_TYPE"));
        try {
            Class.forName(dialect.getDriverClass());
        } catch (Exception e) {
            // do nothing
        }

        return this;
    }

    @Override
    public IDialect getDialect() {
        return this.dialect;
    }

    @Override
    public Connection getConnection() throws SQLException {
        Connection connection = DriverManager.getConnection(url, user, password);
        connection.setAutoCommit(false);
        return connection;
    }
}